import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface, ValidationOptions, registerDecorator } from 'class-validator';
type ModelType = 1 | 2 | 3 | 4 | 5;
@ValidatorConstraint({name:'isPassWord'})
export class PassWardConstraint implements ValidatorConstraintInterface{
    validate(value: any, arg?: ValidationArguments): boolean | Promise<boolean> {
        let valuePassWardType:ModelType=arg.constraints[0]||1
        switch (valuePassWardType) {
            // 必须由大写或小写字母组成(默认模式)
            case 1:
                return /\d/.test(value) && /[a-zA-Z]/.test(value);
            // 必须由小写字母组成
            case 2:
                return /\d/.test(value) && /[a-z]/.test(value);
            // 必须由大写字母组成
            case 3:
                return /\d/.test(value) && /[A-Z]/.test(value);
            // 必须包含数字,小写字母,大写字母
            case 4:
                return /\d/.test(value) && /[a-z]/.test(value) && /[A-Z]/.test(value);
            // 必须包含数字,小写字母,大写字母,特殊符号
            case 5:
                return (
                    /\d/.test(value) &&
                    /[a-z]/.test(value) &&
                    /[A-Z]/.test(value) &&
                    /[!@#$%^&]/.test(value)
                );
            default:
                return /\d/.test(value) && /[a-zA-Z]/.test(value);
        }
    }
    defaultMessage?(validationArguments?: ValidationArguments): string {
        let {value} =validationArguments
        return `${value} 不符合 手机号验证条件`
    }
    
}


export function isPassWord(Model:ModelType,ValidationOptions:ValidationOptions){

    return (obj:Record<string,any>,propertyName:string)=>{
    registerDecorator({
        target: obj.constructor,
        propertyName,
        options:ValidationOptions,
        validator:isPassWord,
        constraints:[Model]

    })


    }

}